<?php

class InventoryItemManager implements InventoryManager
{
    private $selectCount;

    public function SelectAll()
    {
        $query = "SELECT * FROM inventory";
        $result = Core::DBConn()->Query($query);
        $this->selectCount = $result->Count();
        return array_map("self::CreateInventoryItem", $result->GetAllAssoc());
    }

    public static function SelectById($id)
    {
        $query = "SELECT * FROM inventory WHERE inventory_id = %s";
        $db = Core::DBConn();
        $queryResult = $db->Query($query, $db->Quote($id));
        return self::CreateInventoryItem($queryResult->GetAssoc());
    }

    public static function SelectByClientId($clientId)
    {
        $query = "SELECT inventory.* FROM inventory, inventory_rent
                  WHERE
                    inventory.inventory_id = inventory_rent.inventory_id
                      AND
                    inventory_rent.client_id = %s;";
        $db = Core::DBConn();
        $queryResult = $db->Query($query, $db->Quote($clientId));
        return array_map("self::CreateInventoryItem", $queryResult->GetAllAssoc());
    }

    public function GetSelectCount()
    {
        return $this->selectCount;
    }

    public function Insert(InventoryItem $inventoryItem)
    {
        $query = "INSERT INTO inventory
                  SET
                    name = @name,
                    lifetime_in_days = @lifetime_in_days,
                    left_in_stock = @left_in_stock,
                    country_of_production = @country_of_production,
                    cost_per_day = @cost_per_day;";
        $db = Core::DBConn();
        $substitutions = array(
            '@name' => $db->Quote($inventoryItem->GetName()),
            '@lifetime_in_days' => $db->Quote($inventoryItem->GetLifeTimeInDays()),
            '@left_in_stock' => $db->Quote($inventoryItem->GetLeftInStock()),
            '@country_of_production' => $db->Quote($inventoryItem->GetCountryOfProduction()),
            '@cost_per_day' => $db->Quote($inventoryItem->GetCostPerDay())
        );
        $db->Query($query, $substitutions);
    }

    public function Update(InventoryItem $inventoryItem)
    {
        $query = "UPDATE inventory
                  SET
                    name = @name,
                    lifetime_in_days = @lifetime_in_days,
                    left_in_stock = @left_in_stock,
                    country_of_production = @country_of_production,
                    cost_per_day = @cost_per_day
                  WHERE
                    inventory_id = @id;";
        $db = Core::DBConn();
        $substitutions = array(
            '@id' => $db->Quote($inventoryItem->GetId()),
            '@name' => $db->Quote($inventoryItem->GetName()),
            '@lifetime_in_days' => $db->Quote($inventoryItem->GetLifeTimeInDays()),
            '@left_in_stock' => $db->Quote($inventoryItem->GetLeftInStock()),
            '@country_of_production' => $db->Quote($inventoryItem->GetCountryOfProduction()),
            '@cost_per_day' => $db->Quote($inventoryItem->GetCostPerDay())
        );
        $db->Query($query, $substitutions);
    }

    public function Delete($itemId)
    {
        $query = "DELETE FROM inventory WHERE inventory_id = %s";
        $db = Core::DBConn();
        $db->Query($query, $db->Quote($itemId));
    }

    private static function CreateInventoryItem($dbRecord)
    {
        $inventoryItem = new InventoryItem();
        $inventoryItem->SetId($dbRecord['inventory_id']);
        $inventoryItem->SetName($dbRecord['name']);
        $inventoryItem->SetLifeTimeInDays($dbRecord['lifetime_in_days']);
        $inventoryItem->SetLeftInStock($dbRecord['left_in_stock']);
        $inventoryItem->SetCountryOfProduction($dbRecord['country_of_production']);
        $inventoryItem->SetCostPerDay($dbRecord['cost_per_day']);
        return $inventoryItem;
    }


}